﻿/// <reference path="Controls/BaseComplexControl.js" />

TA.Turbo.Controls.AutoCompleteExtender = function AutoCompleteExtender(clientId)
{
	this.BaseComplexControl(clientId);
}
var $AutoCompleteExtender = TA.Turbo.Controls.AutoCompleteExtender;
$AutoCompleteExtender.Inherit($BaseComplexControl);
var _____a;
if (_____a)
{
	$AutoCompleteExtender.prototype = new $BaseComplexControl("");
	$AutoCompleteExtender.prototype.BaseComplexControl = new $BaseComplexControl("");
}

$PropertyDefinition.Define($AutoCompleteExtender, "ListCssClass", "LC", "autocomplete_list");
$PropertyDefinition.Define($AutoCompleteExtender, "MinimumPrefixLength", "MP", 2);
$PropertyDefinition.Define($AutoCompleteExtender, "CompletionTimeout", "CT", 500);
$PropertyDefinition.Define($AutoCompleteExtender, "TargetControlId", "TC", "");

$AutoCompleteExtender.DefineEvent(new $ServerEvent("GetSuggestions"));

$AutoCompleteExtender.prototype._ulSuggestions = document.getElementById("");
$AutoCompleteExtender.prototype._listShown = false;
$AutoCompleteExtender.prototype._ajaxTimeoutId = null;
$AutoCompleteExtender.prototype._timeoutId = null;
$AutoCompleteExtender.prototype._ownerName = "";
$AutoCompleteExtender.prototype._selected = document.getElementById("");
$AutoCompleteExtender.prototype._targetControl = document.getElementById("");

$AutoCompleteExtender.prototype._suggestions = new Array();

$AutoCompleteExtender.prototype.get_TargetControl = function get_TargetControl()
{
	if (this._targetControl == null)
		this._targetControl = $(this.get_TargetControlId());
	return this._targetControl;
}

$AutoCompleteExtender.prototype.Load = function Load()
{
	this.BaseComplexControl.Load.call(this);

	TA.Turbo.Controls.AutoCompleteExtender.AllSpans.push(this.get_Control());
	this.get_Control().style["position"] = "relative";

	if (this.get_TargetControl() == null)
		return;
	this.get_TargetControl().setAttribute("autocomplete", "off");
	this.get_Control().appendChild(this.get_TargetControl());

	this._ulSuggestions = this.get_Control().FindChildren("UL")[0];
	this._ulSuggestions.className = this.get_ListCssClass();
	this._ulSuggestions.style["display"] = "none";
	this._ulSuggestions.style["position"] = "absolute";
	this._ulSuggestions.style["zIndex"] = "10";
	this._ulSuggestions.style["overflowY"] = "auto";
	this.get_Control().appendChild(this._ulSuggestions);

	this.get_TargetControl().AddEvent("keypress", this._Control_KeyPress, this);
}

$AutoCompleteExtender.prototype._Control_KeyPress = function _Control_KeyPress(e)
{
	var k = Events.GetKeyCode(e);
	switch (k)
	{
		case 27:
			this.set_ListShown(false);
			break;
		case 13:
			this._Commit();
			break;
		case 38: // up
			if (this._selected.previousSibling != null)
				this.set_Selected(this._selected.previousSibling);
			else
				this.set_Selected(this._ulSuggestions.childNodes[this._ulSuggestions.childNodes.length - 1]);
			break;
		case 40: // down
			if (this._selected.nextSibling != null)
				this.set_Selected(this._selected.nextSibling);
			else
				this.set_Selected(this._ulSuggestions.childNodes[0]);
			break;
		default:
			if (k == 188
				|| (k >= 190 && k <= 192)
				|| (k >= 48 && k <= 57)
				|| k == 59
				|| k == 107 || k == 109
				|| (k >= 65 && k <= 90)
				|| (k >= 97 && k <= 122)
				|| (k >= 219 && k <= 222)
				|| k == 8)
				setTimeout(this._OnTextChanged.bind(this), 0);

			break;
	}
}
$AutoCompleteExtender.prototype._OnTextChanged = function _OnTextChanged(e)
{
	if (this.get_TargetControl().value.length < this.get_MinimumPrefixLength())
	{
		if (this._timeoutId != null)
			clearTimeout(this._timeoutId);
		this._timeoutId = null;

		if (this._ajaxTimeoutId != null)
			clearTimeout(this._ajaxTimeoutId);
		this._ajaxTimeoutId = null;

		this.set_ListShown(false);
	}
	else
	{
		if (this._timeoutId != null)
			clearTimeout(this._timeoutId);
		this._timeoutId = setTimeout(this._ShowSuggestions.bind(this), this.get_CompletionTimeout());

		this._suggestions = null;
		this._timeoutComplete = false;
		this._selected = null;

		if (this._ajaxTimeoutId != null)
			clearTimeout(this._ajaxTimeoutId);
		this._ajaxTimeoutId = setTimeout(this._DoAjaxRequest.bind(this), this.get_CompletionTimeout() - 200);
	}
}
$AutoCompleteExtender.prototype._Control_MouseClick = function _Control_MouseClick(e)
{
	Events.Stop(e);
}
$AutoCompleteExtender.prototype._document_MouseClick = function _document_MouseClick(e)
{
	this.set_ListShown(false);
}
$AutoCompleteExtender.prototype._DoAjaxRequest = function _DoAjaxRequest()
{
	this._ajaxTimeoutId = null;
	this.RaiseEvent("GetSuggestions", { Argument: this.get_TargetControl().value, CallBack: this._Ajax_CallBack.bind(this) });
}
$AutoCompleteExtender.prototype._Ajax_CallBack = function _Ajax_CallBack(response)
{
	this._suggestions = response.Custom;
	this._CheckIfReady();
}
$AutoCompleteExtender.prototype._ShowSuggestions = function _ShowSuggestions()
{
	this._timeoutComplete = true;
	this._CheckIfReady();
}
$AutoCompleteExtender.prototype._CheckIfReady = function _CheckIfReady()
{
	if (this._timeoutComplete && this._suggestions != null)
	{
		while (this._ulSuggestions.childNodes.length > 0)
			this._ulSuggestions.removeChild(this._ulSuggestions.childNodes[0]);

		for (var i = 0; i < this._suggestions.length; ++i)
		{
			var li = document.createElement("LI");
			li.innerHTML = this._suggestions[i];
			li.AddEvent("click", this.Suggestion_MouseClick.bind(this));
			li.AddEvent("mouseover", this.Suggestion_MouseOver.bind(this));
			this._ulSuggestions.appendChild(li);
		}

		if (this._ulSuggestions.childNodes.length > 0)
			this.set_Selected(this._ulSuggestions.childNodes[0]);
		this.set_ListShown(this._suggestions.length > 0);
	}
}
$AutoCompleteExtender.prototype.Suggestion_MouseOver = function Suggestion_MouseOver(e)
{
	var sender = Events.GetSender(e);
	this.set_Selected(sender);
}
$AutoCompleteExtender.prototype.Suggestion_MouseClick = function Suggestion_MouseClick(e)
{
	this._Commit();
	Events.Stop(e);
}

$AutoCompleteExtender.prototype.set_ListShown = function set_ListShown(value)
{
	this._listShown = value;

	for (var i = 0; i < $AutoCompleteExtender.AllSpans.length; ++i)
	{
		var sp = $AutoCompleteExtender.AllSpans[i];
		sp.style["position"] = value && (sp != this.get_Control()) ? "" : "relative";
	}

	this._ulSuggestions.style["display"] = this._listShown ? "block" : "none";
	
	if (this._listShown)
	{
		this._ulSuggestions.set_Width(this.get_TargetControl().offsetWidth);
		this._ulSuggestions.style["left"] = this.get_TargetControl().offsetLeft + "px";
		this._ulSuggestions.style["top"] = (this.get_TargetControl().offsetTop + this.get_TargetControl().offsetHeight) + "px";

		this.get_TargetControl().AddEvent("click", this._Control_MouseClick, this);
		document.AddEvent("click", this._document_MouseClick, this);
	}
	else
	{
		this.get_TargetControl().RemoveEvent("click", this._Control_MouseClick, this);
		document.RemoveEvent("click", this._document_MouseClick, this);
	}
}
$AutoCompleteExtender.prototype.set_Selected = function set_Selected(selected)
{
	if (this._selected != null)
		this._selected.RemoveClass("selected");
	this._selected = selected;
	if (this._selected != null)
	{
		this._selected.AddClass("selected");
		var top = this._selected.offsetTop - this._ulSuggestions.childNodes[0].offsetTop;
		if (top < this._ulSuggestions.scrollTop)
			this._ulSuggestions.scrollTop = top;
		if (top > this._ulSuggestions.scrollTop + this._ulSuggestions.offsetHeight - this._selected.offsetHeight)
			this._ulSuggestions.scrollTop = top - this._ulSuggestions.offsetHeight + this._selected.offsetHeight;

	}
}
$AutoCompleteExtender.prototype._Commit = function _Commit()
{
	if (this._selected != null)
	{
		this.set_ListShown(false);
		this.get_TargetControl().value = this._selected.innerHTML;
		this.get_TargetControl().focus();
	}
}


$AutoCompleteExtender.AllSpans = new Array();